<?php
class AudienceModel extends Model{
	public function get_admin($id){
		return M('admin')->where('admin_id='.$id)->getField("admin_name");
	}
	public function get_name_by_id($id){
		return M('audience','','DB_MEETING')->where('audience_id='.$id)->getField('audience_name');	
	}
	
	public function add_audience($data){
		$data['time_insert'] 	= time();
		$data['time_update'] 	= time();
		$data['time_delete'] 	= -1;
		$data['audience_state'] = 1;
		//add audience  
		$rs = M('audience','','DB_MEETING')->add($data);
		$amc['category_id'] = $data['category_id'];
		$amc['audience_id'] = $rs;
		$re = M('audience_meeting_category' , '' , 'DB_MEETING') ->add($amc);
		
		if(!$rs)
			return false;
		//add  rds
		$this->add_department_rds($rs,$data['audience_department']);
		//add admin
		if($data['audience_rights']!=1){
			$data1['admin_name']  	 = $data['audience_username'];
			$data1['admin_pwd']	  	 = $data['audience_pwd'];
			$data1['admin_rights_id']= $data['audience_rights']==2?2:1;
			$data1['meeting']		 = $data['meeting'];
			D('Admin')->add_admin($data1);
		}

		return $rs && $re;
	}
	public function add_type($data){

	}
	
	public function del_audience($id){
		return M('audience','','DB_MEETING')->where('audience_id='.$id)->delete();
	
	}
	
	// 李记辉  添加于2015/01/26
	public function upt_audience($data){
		$rs = M('audience','','DB_MEETING')->where('audience_id='.$data['audience_id'])->save($data);
		
		$rs1 = M('audience_meeting_category','','DB_MEETING')->where('audience_id='.$data['audience_id'])->find();
		if(!$rs1)
		{	
			// 如果当前用户没有会议分组，则在修改时直接添加分组
			$re = M('audience_meeting_category','','DB_MEETING')->where('audience_id='.$data['audience_id'])->add($data);
		}else{
			// 如果当前用户有会议分组，则在修改时直接修改分组
			$re = M('audience_meeting_category','','DB_MEETING')->where('audience_id='.$data['audience_id'])->save($data);
		}
		return $rs && $re;
	}
	// 李记辉  添加于2015/01/26
	
	public function get_data_by_audience_id($id){
		return M('audience','','DB_MEETING')->where('audience_id='.$id)->find();
	}

	// 李记辉  添加于2015/01/26
	public function get_audience_meeting_category($id){
		$rs = M('audience_meeting_category','','DB_MEETING')->where('audience_id='.$id)->find();
		return M('meeting_category' , '' , 'DB_MEETING')->where('category_id='.$rs['category_id'])->find();
	}
	// 李记辉  添加于2015/01/26

	public function get_users(){
		return M('audience','','DB_MEETING')->select();	
	}
//查询多条数据
	public function get_data($mysql,$a,$b,$c,$d){
		return M($mysql,'','DB_MEETING')->field($a,$b,$c,$d)->select();
	}	
//添加数据
	public function add_data($mysql,$data){
		return M($mysql,'','DB_MEETING')->add($data);
	}

//删除数据	
	public function del_data($mysql,$id,$id2){
		return M($mysql,'','DB_MEETING')->where(array($id=>$id2))->delete();
	
	}
//查询数据	
	public function find_data($mysql,$id,$val){
		return M($mysql,'','DB_MEETING')->where(array($id=>$val))->find();
	}
//修改数据	
	public function save_data($mysql,$id,$val,$data){
		return M($mysql,'','DB_MEETING')->where(array($id=>$val))->save($data);
	}
	
	public function get_audience_group_by_department(){
		$sql = 'select a.audience_id,a.audience_name,a.audience_department,b.department_name from audience a';
		$sql.= ' join audience_department as b on a.audience_department = b.department_id';
		$sql.= ' order by a.audience_department';
		$rs =  M($mysql,'','DB_MEETING')->query($sql);
		$newarr = array();
		foreach($rs as $val){
			$newarr[$val['department_name']][] = $val;	
		}
		return $newarr;
	}

	public function add_department_rds($uid,$department_id)
	{
		$data['audience_id']  = $uid;
		$data['department_id']= $department_id;
		$data['time_insert']  = time();
		$data['time_update']  = time();
		$data['time_delete']  = -1;
		M('audience_department_rds','','DB_MEETING')->add($data);
	}

	public function update_department_rds($uid,$old_department_id,$department_id)
	{
		$data['time_delete'] = time();
		M('audience_department_rds','','DB_MEETING')->where('audience_id='.$uid.' and department_id='.$old_department_id.' and time_insert>time_delete')->save($data);
		$this->add_department_rds($uid,$department_id);
	}

	public function get_meeting_categorys(){
		return M('meeting_category' , '' , 'DB_MEETING') ->where('time_insert>time_delete') ->select();
	}
}

	
?>